﻿@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section styles{
    <style>
    </style>
}

<div class="layui-card">
    <div class="layui-card-body layui-row layui-col-space10">
        <div class="layui-tab layui-tab-brief" lay-filter="role-tab">

            <div class="layui-tab layui-tab-brief" lay-filter="user-tab">
                <ul class="layui-tab-title">
                    <li class="layui-this">基本信息</li>
                    <li>角色信息</li>
                </ul>
                <div class="layui-tab-content">
                    <div class="layui-tab-item layui-show">

                        <form class="layui-form" lay-filter="user-form" style="padding: 15px 0 0 0;">
                            <input type="hidden" name="Id" value="@Model.Id" />
                            <input type="hidden" name="CompanyName" id="CompanyName" value="@Model.CompanyName" />
                             <input type="hidden" name="CompanyId" id="CompanyId" value="@Model.CompanyId" />
                          
                            <div class="layui-form-item">
                                <label class="layui-form-label layui-form-required">登录账号</label>
                                <div class="layui-input-block">
                                    <input type="text" id="UserName" name="UserName" @(Model.Id > 0 ? "readonly" : "") lay-verify="required" placeholder="请输入登录账号，只能是手机号" autocomplete="off" class="layui-input" value="@Model.UserName">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label layui-form-required">用户密码</label>
                                <div class="layui-input-block">
                                    @if (Model.Id > 0)
                                    {
                                        <input type="password" name="UserPwd" lay-verify="UserPassword" placeholder="密码为空不修改" autocomplete="off" class="layui-input" value="@Model.UserPwd">
                                    }
                                    else
                                    {
                                        <input type="password" name="UserPwd" lay-verify="required|UserPassword" placeholder="请输入密码" autocomplete="off" class="layui-input" value="@Model.UserPwd">
                                    }
                                </div>
                            </div>

                            <div class="layui-form-item">
                                <label class="layui-form-label layui-form-required">姓名</label>
                                <div class="layui-input-block">
                                    <input type="text" name="RealName" lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input" value="@Model.RealName">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label layui-form-required">用户类型</label>
                                <div class="layui-input-inline">
                                    <select name="UserType" lay-verify="required" lay-filter="usertype">
                                        @if (ViewBag.UserTypeList != null)
                                        {
                                            foreach (var item in ViewBag.UserTypeList)
                                            {
                                                <option value="@item.Dvalue" @(item.Dvalue == Model.UserType.ToString() ? "selected" : "")>@(item.Dname)</option>
                                            }
                                        }
                                    </select>
                                </div>
                                <div class="layui-input-inline" id="selcompany"  style="width:260px;">
                                    
                                  <ul id="orgTree" class="dtree" data-id="0"></ul>
                                </div>
                            </div>

                            <div class="layui-form-item">
                                <label class="layui-form-label layui-form-required">手机号</label>
                                <div class="layui-input-block">
                                    <input type="text" id="Mobile" name="Mobile" lay-verify="required|Phone" placeholder="请输入手机号" autocomplete="off" class="layui-input" maxlength="11" value="@Model.Mobile">
                                </div>
                            </div>
                            <div class="layui-form-item layui-hide">
                                <label class="layui-form-label layui-form-required">邮箱</label>
                                <div class="layui-input-block">
                                    <input type="text" name="Email"  placeholder="请输入邮箱" autocomplete="off" class="layui-input" value="@Model.Email">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">启用</label>
                                <div class="layui-input-block">
                                    <input type="checkbox" name="isenable" lay-skin="switch" @(Model.Isenable ? "checked" : "")>
                                </div>
                            </div>

                            <div class="layui-form-item">
                                <div class="layui-input-block">
                                    <button class="layui-btn layui-hide" lay-submit lay-filter="user-submit" id="user-submit">提交</button>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="layui-tab-item">
                        <div class="layui-col-md12 layui-form" id="rolelist">
                            @if (ViewBag.RoleList != null)
                            {
                                foreach (var item in ViewBag.RoleList)
                                {
                                   
                                    <input type="checkbox" name="userrole" lay-skin="primary" value="@item["RoleCode"]" title="@item["RoleName"]" @(item["Checked"].ToString()=="1"?"checked":"")>
                                }
                            }
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>


@section scripts{
    <script type="text/javascript">
        layui.use(['lscom', 'form','select','dtree'], function () {
            var $ = layui.$;
            var lscom = layui.lscom;
            var form = layui.form;
            let select = layui.select;
            var dtree = layui.dtree;
            var index = parent.layer.getFrameIndex(window.name);
            //parent.layer.iframeAuto(index);

            dtree.render({
                elem: "#orgTree",
                initLevel: "1",
                method: 'get',
                url: "/Systems/Organize/GetdtreeData?id=0",
                icon: ["0", "-1"],
                response: { message: "msg", statusCode: 0 },
                dataStyle: "layuiStyle",  //使用layui风格的数据格式
                dataFormat: "list",  //配置data的风格为list
                select: true,
                width:"200",
                selectInitVal: "@Model.CompanyId", 
                selectTips:'请选择部门'
            });
               // 绑定节点点击
            dtree.on("node('orgTree')", function (obj) {
                $("#CompanyId").val(obj.param.nodeId);
            });
            window.submitForm = function () {
                $("#user-submit").click();
            }
            //$("#UserName").blur(function () {
            //    $("#Mobile").val($("#UserName").val());
            //});
             //自定义验证规则
            form.verify({
                UserName: function (value) {
                    if (lscom.utils.getstrlength(value) > 40) {
                        return '超过20个字符限制';
                    } else {
                        var msg = '';
                        lscom.ajax({
                            url: "/Systems/User/ExistsUserName",
                            data: JSON.stringify({ id: '@Model.Id', userName: value }),
                            async: false,
                            lsHandleError: false
                        }).done(function (data) {
                            if (data.code == 1) {
                                msg = data.message;
                            }
                        }).fail(function (message) {
                            lscom.notify.error(message);
                        })
                        return msg;
                    }
                },
                UserPassword: function (value) {
                    if (value!="" && !/(?=.{8,16})(?=.*[a-z])(?=.*[A-Z])(?=.*\d).*/.test(value)) {
                        return '密码包含大小写字母和数字，并且长度在8-16之间';
                    }
                },
                UserPhone: function (value) {
                    if (!/^1\d{10}$/.test(value)) {
                        return '请输入登录账号只能为手机号';
                    }
                },
                Phone: function (value) {
                    if (!/^1\d{10}$/.test(value)) {
                        return '请输入正确的手机号';
                    }
                },
                Email: function (value) {
                   
                    if (value!="" && !/^([a-zA-Z0-9_\.\-])+\@@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(value)) {
                        return '请输入正确邮箱格式';
                    }
                }
            });

           
            form.on('select(CompanyId)', function (data) {
               
                $("#CompanyName").val(data.elem.options[data.elem.selectedIndex].text);
            });
            form.on('submit(user-submit)', function (data) {
                //如果选择服务商类型需选择服务商
                //if (data.field.CompanyId == "") {
                //    parent.layer.msg("请选择所属服务商", { icon: 5 });
                //    return false;
                //}
                
                let rolecodearry = [];

                $('#rolelist input[name="userrole"]:checked').each(function () {
                    rolecodearry.push($(this).val());
                });
                data.field.RoleCode = rolecodearry.join(',');

                data.field.isenable = data.field.isenable == "on";
               
                if (data.field.CompanyId == "") {
                    data.field.CompanyId = 0;
                }

                var field = data.field;
               
                var waitIndex = parent.layer.load(2);
                lscom.ajax({
                    url: "/Systems/User/SaveUser",
                    data: JSON.stringify(field),
                    lsHandleError: false
                }).done(function (data) {
                    if (data.code == 0) {
                        data.index = index;
                        let parentWindow = parent.selectedWindow().window;
                        parentWindow.saveCallback(data);
                    }
                }).fail(function (message) {
                    lscom.notify.error(message);
                }).always(function () {
                    parent.layer.close(waitIndex);
                });

                return false;
            });
        });
    </script>
  
}

